package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Toast;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private CurrencyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CurrencyAdapter(this::navigateToCalculate);
        recyclerView.setAdapter(adapter);
        loadCurrencyData();
    }

    private void loadCurrencyData() {
        CurrencyApiService.fetchAndParseData(new CurrencyApiService.ParserCallback() {
            @Override
            public void onDataReady(List<Currency> currencyList) {
                runOnUiThread(() -> adapter.setData(currencyList));
            }

            @Override
            public void onError(String message) {
                runOnUiThread(() -> Toast.makeText(
                        MainActivity.this,
                        message,
                        Toast.LENGTH_LONG
                ).show());
            }
        });
    }
    private void navigateToCalculate(Currency currency) {
        CalculateActivity.start(this, currency);
    }
}